package com.bootdo.appsystem.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.appsystem.service.RegectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.appsystem.domain.UserdegreeDO;
import com.bootdo.appsystem.service.UserdegreeService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author xiaoxiao
 * @email 806191255@qq.com
 * @date 2018-05-12 14:42:14
 */
 
@Controller
@RequestMapping("/appsystem/userdegree")
public class UserdegreeController {
	@Autowired
	private UserdegreeService userdegreeService;
	@Autowired
	private RegectService regectService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:userdegree:userdegree")
	String Userdegree(){
	    return "appsystem/userdegree/userdegree";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:userdegree:userdegree")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserdegreeDO> userdegreeList = userdegreeService.list(query);
		int total = userdegreeService.count(query);
		PageUtils pageUtils = new PageUtils(userdegreeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("appsystem:userdegree:add")
	String add(){
	    return "appsystem/userdegree/add";
	}

	@GetMapping("/edit/{degreeid}")
	@RequiresPermissions("appsystem:userdegree:edit")
	String edit(@PathVariable("degreeid") Integer degreeid,Model model){
		UserdegreeDO userdegree = userdegreeService.get(degreeid);
		model.addAttribute("userdegree", userdegree);
	    return "appsystem/userdegree/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:userdegree:add")
	public R save( UserdegreeDO userdegree){
		userdegree.setUid(userdegree.getUid()-10000);
		userdegree.setAddtime(new Date());
		if(userdegree.getRegectid() != 0) {
			userdegree.setRegectname(regectService.get(userdegree.getRegectid().doubleValue()).getRegionName());
		}else {
			userdegree.setRegectid(null);
			userdegree.setRegectname(".");
		}
		if(userdegreeService.save(userdegree)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:userdegree:edit")
	public R update( UserdegreeDO userdegree){
		userdegree.setUid(userdegree.getUid()-10000);
		if(userdegree.getRegectid() != 0) {
			userdegree.setRegectname(regectService.get(userdegree.getRegectid().doubleValue()).getRegionName());
		}else {
			userdegree.setRegectid(null);
			userdegree.setRegectname(".");
		}
		userdegreeService.update(userdegree);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:userdegree:remove")
	public R remove( Integer degreeid){
		if(userdegreeService.remove(degreeid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:userdegree:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] degreeids){
		userdegreeService.batchRemove(degreeids);
		return R.ok();
	}
	
}
